package charles.testproject.googleapi;

import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import java.io.IOException;
import java.util.Iterator;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

public class GoogleCalendarFeedsHttp {
	
	public static void main(String[] args) throws Exception{
		
	SyndFeedInput input = new SyndFeedInput();
	
	URL urlCalendar = new URL("http://www.google.com/calendar/feeds/sancho.lai%40gmail.com/public/basic");
	URLConnection urlConn = urlCalendar.openConnection();
	
	//set the sequence of language that web service response
	urlConn.setRequestProperty("Accept-Language", "en-us,zh-cn;q=0.7,en;q=0.3");
	
	XmlReader xmlReader = new XmlReader(urlConn);
	System.out.println(xmlReader.getEncoding());
    SyndFeed feed = input.build(xmlReader);

    // Iterate through feed items, adding a footer each item
    Iterator entryIter = feed.getEntries().iterator();
    while (entryIter.hasNext()) {
        SyndEntry entry = (SyndEntry) entryIter.next();
        
        List contentList = entry.getContents();
        String contentStr = contentList.toString();
        int i = contentStr.indexOf("=");
        int j = contentStr.indexOf("<");
        contentStr = contentStr.substring(i + 1, j);
        
        System.out.println(contentStr);
               
        System.out.println(entry.getTitle());

        System.out.println("\n***********edge for every loop*************\n");
	}
	}
}
